<?php
/*
 * Copyright (C) 2004-2011 wortzwei GmbH <info@wortzwei.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @since 14.01.2011
 * @author Dirk Dittmar
 */

require_once( 'core.php' );
require_once( 'compress_api.php' );
require_once( 'html_api.php' );
require_once( 'current_user_api.php' );
require_once( 'bug_api.php' );
require_once( 'string_api.php' );
require_once( 'date_api.php' );
require_once( 'icon_api.php' );
require_once( 'columns_api.php' );

require_once( dirname(dirname(__FILE__)) . '/inc/SolrSearch_Helpers.class.php' );
require_once( dirname(dirname(__FILE__)) . '/inc/SolrSearch_SolrService.class.php' );

auth_ensure_user_authenticated();

$page_number = isset($_GET['page_number']) ? $_GET['page_number'] : 1;
$num_rows = 10;

if (isset($_GET['query'])) {
	$solr = new SolrSearch_SolrService(
					plugin_config_get(SolrSearchPlugin::SOLR_HOST),
					plugin_config_get(SolrSearchPlugin::SOLR_PORT),
					plugin_config_get(SolrSearchPlugin::SOLR_PATH)
				);
	$query = $_GET['query'];

	$solrException = null;
	try {
		$solrResponse = $solr->search($query, $page_number, 10, helper_get_current_project());
	} catch (Exception $e) {
		$solrException = $e;
	}
}

if (isset($_GET['luck']) && isset($solrResponse)) {
	if (array_key_exists(0, $solrResponse->response->docs)) {
		print_header_redirect_view($solrResponse->response->docs[0]->id);
	}
}

if (isset($_GET['query']) && isset($solrResponse)) {
	$docs = $solrResponse->response->docs;
	$bug_count = $solrResponse->response->numFound;

	$columns = helper_get_columns_to_view( COLUMNS_TARGET_VIEW_PAGE );

	// remove 'selection' and 'edit'
	foreach ($columns as $key => $value) {
		if ($value === 'selection') {
			unset($columns[$key]);
			break;
		}
	}
	foreach ($columns as $key => $value) {
		if ($value === 'edit') {
			unset($columns[$key]);
			break;
		}
	}

	// add column score
	if (plugin_config_get(SolrSearchPlugin::SOLR_SCORE)) {
		array_unshift($columns, plugin_lang_get('solr_score'));
	}

	$col_count = count($columns);

	$bugs = array();
	foreach ($docs as $doc) {
		// get the bug by id
		$bug = bug_get($doc->id);
		$bug->solrScore = $doc->score;
		$bugs[] = $bug;
	}

	$v_start = 0;
	$v_end = 0;
	if ( count( $docs ) > 0 ) {
		$v_start = $num_rows * ($page_number - 1) + 1;
		$v_end = $v_start + count( $docs ) - 1;
	}
}

compress_enable();
html_page_top(plugin_lang_get('title'));

?>
<div class="center">
	<form method="GET" action="<?php echo plugin_page( 'search' ) ?>">
		<img alt="powerd by Apache-Solr" width="283" height="156" src="<?php echo plugin_file('solr.jpg'); ?>">
		<br />
		<input type="text" name="query" size="55" value="<?php echo isset($_GET['query']) ? $_GET['query'] : '' ?>" />
		<input type="hidden" name="page" value="<?php echo $_GET['page'] ?>" />
		<input type="hidden" name="page_number" value="1" />
		<br />
		<input type="submit" name="search" value="<?php echo plugin_lang_get('search') ?>" />
		<input type="submit" name="luck" value="<?php echo plugin_lang_get('imfeelinglucky') ?>" />
	</form>
<?php if (isset($solrException)) : ?>
	<p><font color="red"><?php echo $solrException->getMessage() ?></font></p>
<?php endif; ?>
</div>
<?php if (isset($_GET['query']) && isset($solrResponse)) : ?>
<div class="center">
<?php
$t_status_legend_position = config_get( 'status_legend_position' );
if ( $t_status_legend_position == STATUS_LEGEND_POSITION_TOP || $t_status_legend_position == STATUS_LEGEND_POSITION_BOTH ) {
	html_status_legend();
}
?>
</div>
<br />
<table id="buglist" class="width100" cellspacing="1">
	<tr>
		<td class="form-title" colspan="<?php echo $col_count; ?>">
			<span class="floatleft">
			<?php
			echo lang_get( 'viewing_bugs_title' );
			echo " ($v_start - $v_end / $bug_count)";
			?>
			</span>
			<span class="floatright small">
			<?php
				SolrSearch_Helpers::print_page_links( (int)($bug_count / $num_rows) + 1, (int)$page_number );
			?>
			</span>
		</td>
	</tr>
	<tr class="row-category">
	<?php
	foreach( $columns as $column ) {
		SolrSearch_Helpers::print_column_title($column);
	}
	?>
	</tr>
	<tr class="spacer">
		<td colspan="<?php echo $col_count; ?>"></td>
	</tr>
	<?php foreach ($bugs as $bug) :	?>
	<tr bgcolor="<?php echo get_status_color( $bug->status ) ?>" valign="top">
	<?php
	foreach( $columns as $column ) {
		SolrSearch_Helpers::print_column_value( $column, $bug, plugin_lang_get('solr_score') );
	}
	?>
	</tr>
	<?php endforeach; ?>
</table>
<?php
if ( $t_status_legend_position == STATUS_LEGEND_POSITION_BOTTOM || $t_status_legend_position == STATUS_LEGEND_POSITION_BOTH ) {
	html_status_legend();
}
endif;

html_page_bottom();

